package jianzhi

func levelOrder2(root *TreeNode) [][]int {

	res := [][]int{}

	if root == nil {
		return res
	}

	stack := []*TreeNode{root}

	for len(stack) > 0 {
		levelLen := len(stack)
		level := []int{}
		for i := 0; i < levelLen; i++ {
			temp := stack[0]
			if temp.Left != nil {
				stack = append(stack, temp.Left)
			}
			if temp.Right != nil {
				stack = append(stack, temp.Right)
			}
			level = append(level, temp.Val)
			stack = stack[1:]
		}

		res = append(res, level)
	}

	return res

}
